<template>
  <a-modal title-align="start" title="批量设置采购员" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="420">
    <a-space>
      <div>请选择采购员：</div>
      <a-select style="width: 260px" allow-search v-model="salesmanId" placeholder="请选择采购员">
        <a-option v-for="(item, index) of userList" :key="index" :label="item.userName" :value="item.id"></a-option>
      </a-select>
    </a-space>

    <template #footer class="modal-footer">
      <a-button @click="showvisible = false">取消</a-button>
      <a-button type="primary" :loading="submitLoading" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { onMounted, ref, computed } from 'vue'
  import { getOrganizesUser } from '@/api/system'
  import { batchUpdatePurchaser } from '@/api/purchase'
  import { Message } from '@arco-design/web-vue'
  let props = defineProps(['modelValue', 'ids', 'info'])
  let emits = defineEmits(['save', 'update:modelValue'])
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })

  let salesmanId = ref('')
  let submitLoading = ref(false)
  let userList = ref<Array<any>>([])
  const getUSerList = async () => {
    let params = {
      depart: false,
      pageNum: 1,
      pageSize: 100000,
    }
    const res: any = await getOrganizesUser(params)
    userList.value = res?.data?.records || []
  }

  const submit = async () => {
    if (!salesmanId.value) return Message.error('请选择运营')
    let userData = userList.value.find((item) => item.id === salesmanId.value)
    let params = {
      idList: props.ids,
      purchaser: {
        deptId: userData?.deptId || '',
        deptName: userData?.deptName || '',
        id: userData?.id || '',
        name: userData?.userName || '',
      },
    }

    try {
      submitLoading.value = true
      const res: any = await batchUpdatePurchaser(params)
      if (res) {
        Message.success('设置采购员成功')
        emits('save', false)
        showvisible.value = false
      }
    } catch (error) {}
    submitLoading.value = false
  }

  onMounted(getUSerList)
</script>

<style lang="less" scoped></style>
